"""
工具方法


"""
import functools

from flask import current_app
from flask import g
from flask import session

from info.models import User


def num_eng(num):
    if num == 1:
        return "first"
    elif num == 2:
        return "second"
    elif num == 3:
        return "third"
    else:
        return ""


# 定义装饰器 获取用户登录
def get_login_data(view_func):
    @functools.wraps(view_func)
    def inner(*args, **kwargs):
        user_id = session.get("user_id")
        # 2、查询数据库 为了拿到用户的相关数据
        user = None
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

        # g变量是此次请求过来以后起作用的全局变量
        g.user = user
        return view_func(*args, **kwargs)

    return inner
